/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.click.app.webapp.action;

import com.click.app.model.AsignationRequestLaundryPay;
import com.click.app.model.BalanceLaundry;
import com.click.app.model.Laundry;
import com.click.app.model.LaundryRequest;
import com.click.app.model.PaymentsLaundry;
import com.click.app.model.PeriodPaymentLaundry;
import com.click.app.service.AsignationRequestLaundryPayManager;
import com.click.app.service.BalanceLaundryManager;
import com.click.app.service.LaundryManager;
import com.click.app.service.LaundryRequestManager;
import com.click.app.service.PaymentsLaundryManager;
import com.click.app.service.PeriodPaymentLaundryManager;
import java.util.List;

/**
 *
 * @author arnold
 */
public class PaymentLaundryAction extends BaseAction {
    private Long id;
    private Laundry laundry; 
    private LaundryManager laundryManager;
    private PeriodPaymentLaundryManager periodPaymentLaundryManager;
    private PeriodPaymentLaundry periodPaymentLaundry;
    private List<BalanceLaundry> laundriesBalances;
    private PaymentsLaundry paymentsLaundry;
    private PaymentsLaundryManager paymentsLaundryManager;
    private BalanceLaundryManager balanceLaundryManager;
    private LaundryRequestManager laundryRequestManager;
    private AsignationRequestLaundryPayManager asignationRequestLaundryPayManager;
    
    public String list() {
        System.out.println("PaymentLaundryAction -> list()");
        if (id != null) {
            laundry = laundryManager.get(id);
            getSession().setAttribute("idLaundry", laundry.getId());
        }
        if (laundry != null) {
            periodPaymentLaundry = periodPaymentLaundryManager.findCurrentPeriodByLaundry(laundry.getId());
            laundriesBalances = periodPaymentLaundry.getBalanceLaundryList();

            System.out.println("PaymentLaundryAction -> end list()");
            return SUCCESS;
        } else {
            System.out.println("PaymentLaundryAction -> error end list()");
            return ERROR;
        }
    }
    public String edit() {
        System.out.println("PaymentLaundryAction -> edit()");
        if(getSession().getAttribute("idLaundry") != null ){
            laundry = laundryManager.get((Long)getSession().getAttribute("idLaundry"));
            periodPaymentLaundry = periodPaymentLaundryManager.findCurrentPeriodByLaundry(laundry.getId());

        }
        System.out.println("PaymentLaundryAction -> end edit()");
        return SUCCESS;
    }

    public String save(){
        System.out.println("PaymentLaundryAction -> save()");
        if(paymentsLaundry.getAmount() != null && paymentsLaundry.getAmount() > 0
          &&  paymentsLaundry.getDatePay() != null &&  !paymentsLaundry.getDatePay().equals("")){
            
            laundry = laundryManager.get((Long)getSession().getAttribute("idLaundry"));
            periodPaymentLaundry = periodPaymentLaundryManager.findCurrentPeriodByLaundry(laundry.getId());
            paymentsLaundry.setPeriodPaymentLaundry(periodPaymentLaundry);
            paymentsLaundry = paymentsLaundryManager.save(paymentsLaundry);
            Double amount = paymentsLaundry.getAmount();

            List<LaundryRequest> lRequest = laundryRequestManager.findRequestLaundryGT0ByPeriod(periodPaymentLaundry.getId());
            for (LaundryRequest laundryRequest : lRequest) {
                AsignationRequestLaundryPay arlp = new AsignationRequestLaundryPay();
                
                arlp.setPaymentsLaundry(paymentsLaundry);
                arlp.setLaundryRequest(laundryRequest);
                if(amount > laundryRequest.getCurrentTotalPrice()){

                    arlp.setAmountPayed(laundryRequest.getCurrentTotalPrice());
                    amount = amount - laundryRequest.getCurrentTotalPrice();
                    laundryRequest.setCurrentTotalPrice(0D);
                }else{
                    if(amount <= laundryRequest.getCurrentTotalPrice()){
                        arlp.setAmountPayed(amount);
                        laundryRequest.setCurrentTotalPrice(laundryRequest.getCurrentTotalPrice() - amount);
                        amount = 0d;
                    }
                }
                laundryRequestManager.save(laundryRequest);
                asignationRequestLaundryPayManager.save(arlp);
                if(amount ==  0)
                    break;
            }
            BalanceLaundry balanceLaundry = new BalanceLaundry();
            balanceLaundry.setAmountWon(0D);
            balanceLaundry.setAmountPaid(paymentsLaundry.getAmount());
            balanceLaundry.setDateBalance(paymentsLaundry.getDatePay());
            balanceLaundry.setBalance(periodPaymentLaundry.getCurrentAmount() - paymentsLaundry.getAmount());
            balanceLaundry.setObservation("PAGO NORMAL");
            balanceLaundry.setPeriodPaymentLaundry(periodPaymentLaundry);
            balanceLaundry.setPaymentsLaundry(paymentsLaundry);
            balanceLaundry.setTypeBalance(3);
            balanceLaundry = balanceLaundryManager.save(balanceLaundry);

            periodPaymentLaundry.setAmountPayment(periodPaymentLaundry.getAmountPayment() + balanceLaundry.getAmountPaid());
            periodPaymentLaundry.setCurrentAmount(periodPaymentLaundry.getCurrentAmount() - balanceLaundry.getAmountPaid());
            periodPaymentLaundryManager.save(periodPaymentLaundry);
            
        }
        System.out.println("PaymentLaundryAction -> end save()");
        return SUCCESS;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Laundry getLaundry() {
        return laundry;
    }

    public void setLaundry(Laundry laundry) {
        this.laundry = laundry;
    }

    public LaundryManager getLaundryManager() {
        return laundryManager;
    }

    public void setLaundryManager(LaundryManager laundryManager) {
        this.laundryManager = laundryManager;
    }

    public List<BalanceLaundry> getLaundriesBalances() {
        return laundriesBalances;
    }

    public void setLaundriesBalances(List<BalanceLaundry> laundriesBalances) {
        this.laundriesBalances = laundriesBalances;
    }

    public PeriodPaymentLaundry getPeriodPaymentLaundry() {
        return periodPaymentLaundry;
    }

    public void setPeriodPaymentLaundry(PeriodPaymentLaundry periodPaymentLaundry) {
        this.periodPaymentLaundry = periodPaymentLaundry;
    }

    public PeriodPaymentLaundryManager getPeriodPaymentLaundryManager() {
        return periodPaymentLaundryManager;
    }

    public void setPeriodPaymentLaundryManager(PeriodPaymentLaundryManager periodPaymentLaundryManager) {
        this.periodPaymentLaundryManager = periodPaymentLaundryManager;
    }

    public PaymentsLaundry getPaymentsLaundry() {
        return paymentsLaundry;
    }

    public void setPaymentsLaundry(PaymentsLaundry paymentsLaundry) {
        this.paymentsLaundry = paymentsLaundry;
    }

    public PaymentsLaundryManager getPaymentsLaundryManager() {
        return paymentsLaundryManager;
    }

    public void setPaymentsLaundryManager(PaymentsLaundryManager paymentsLaundryManager) {
        this.paymentsLaundryManager = paymentsLaundryManager;
    }

    public BalanceLaundryManager getBalanceLaundryManager() {
        return balanceLaundryManager;
    }

    public void setBalanceLaundryManager(BalanceLaundryManager balanceLaundryManager) {
        this.balanceLaundryManager = balanceLaundryManager;
    }

    public LaundryRequestManager getLaundryRequestManager() {
        return laundryRequestManager;
    }

    public void setLaundryRequestManager(LaundryRequestManager laundryRequestManager) {
        this.laundryRequestManager = laundryRequestManager;
    }

    public AsignationRequestLaundryPayManager getAsignationRequestLaundryPayManager() {
        return asignationRequestLaundryPayManager;
    }

    public void setAsignationRequestLaundryPayManager(AsignationRequestLaundryPayManager asignationRequestLaundryPayManager) {
        this.asignationRequestLaundryPayManager = asignationRequestLaundryPayManager;
    }
    
}
